package com.ctg.itrdc.sysmgr.portal.control;

import com.ctg.itrdc.pasp.core.PageInfo;
import com.ctg.itrdc.pasp.web.CommonResult;
import com.ctg.itrdc.pasp.web.control.SimpleController;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemPasswordRuleDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemUserDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.ISystemPasswordRuleService;
import com.ctg.itrdc.sysmgr.portal.api.service.ISystemUserService;
import com.ctg.itrdc.sysmgr.portal.data.entity.SystemPasswordRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by xiongkw on 2017/5/4.
 */
@RestController
@RequestMapping("/password_rule")
public class SystemPasswordRuleController extends SimpleController<SystemPasswordRule, Long, SystemPasswordRuleDTO> {

    @Autowired
    private ISystemUserService systemUserService;

    protected ISystemPasswordRuleService getService() {
        return (ISystemPasswordRuleService) super.getService();
    }

    @RequestMapping(value = "rules", method = RequestMethod.GET)
    public CommonResult query(@RequestParam(required = false) String search,
                              @RequestParam(required = false, defaultValue = "1") int currentPage,
                              @RequestParam(required = false, defaultValue = "10") int perPage) {
        PageInfo<SystemPasswordRuleDTO> page = getService().queryByName(search, currentPage, perPage);
        return new CommonResult(HttpStatus.OK.toString(), "", page);
    }

    @RequestMapping(value = "rules/select", method = RequestMethod.GET)
    public CommonResult select() {
        List<SystemPasswordRuleDTO> list = getService().selectByExample(new SystemPasswordRuleDTO());
        return new CommonResult(HttpStatus.OK.toString(), "", list);
    }

    @RequestMapping(value = "rules/{id}", method = RequestMethod.GET)
    public CommonResult get(@PathVariable Long id) {
        CommonResult result = super.get(id);
        result.setResultCode(HttpStatus.OK.toString());
        return result;
    }

    @RequestMapping(value = "rules/user", method = RequestMethod.GET)
    public CommonResult get(@RequestParam String username) {
        SystemUserDTO dto = systemUserService.findUserByLoginName(username);
        if (dto == null) {
            return new CommonResult(HttpStatus.OK.toString(),"");
        }
        Long passRuleId = dto.getPassRuleId();
        CommonResult result = new CommonResult();
        if (passRuleId != null) {
            SystemPasswordRuleDTO rule = getService().selectByPrimaryKey(passRuleId);
            result.setData(rule);
        }
        result.setResultCode(HttpStatus.OK.toString());
        return result;
    }

    @RequestMapping(value = "rules", method = RequestMethod.POST)
    public CommonResult save(@RequestBody SystemPasswordRuleDTO d) {
        CommonResult result = super.save(d);
        result.setResultCode(HttpStatus.OK.toString());
        return result;
    }

    @RequestMapping(value = "rules", method = RequestMethod.PUT)
    public CommonResult update(@RequestBody SystemPasswordRuleDTO dto) {
        CommonResult result = super.update(dto);
        result.setResultCode(HttpStatus.OK.toString());
        return result;
    }

    @RequestMapping(value = "rules/{id}", method = RequestMethod.DELETE)
    public CommonResult delete(@PathVariable Long id) {
        CommonResult result = super.delete(id);
        result.setResultCode(HttpStatus.OK.toString());
        return result;
    }

    @RequestMapping(value = "rules/exists", method = RequestMethod.GET)
    public CommonResult exists(SystemPasswordRuleDTO dto) {
        CommonResult result = super.exists(dto);
        result.setResultCode(HttpStatus.OK.toString());
        return result;
    }

}